package com.yjd.bi.birthday.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.bi.birthday.model.BirthdayModelDto;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.bi.birthday.model.BirthdayModel;
import com.yjd.bi.birthday.service.IBirthdayService;
import com.yjd.bi.birthday.dao.IBirthdayDao;

/**
 * <b>description</b>：微信分享红包业务实现<br>
 * <b>time</b>：2016-12-14 10:22:21 <br>
 * <b>author</b>：  ready likun_557@163.com
 */
@Service("birthdayService")
public class BirthdayServiceImpl extends BaseServiceImpl implements IBirthdayService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BirthdayModel insert(BirthdayModel model) throws Exception {
		return this.birthdayDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BirthdayModel model) throws Exception {
		return this.birthdayDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.birthdayDao.deleteModel(paramMap, true);
	}

	@Resource
	private IBirthdayDao birthdayDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.birthdayDao;
	}

	@Override
	public Double getValueBySqlName(String name, Map map) {
		return this.birthdayDao.getValueBySqlName(name,map);
	}

	@Override
	public List<BirthdayModelDto> getValueBySqlName1(String name, Map map) {
		return this.birthdayDao.getValueBySqlName1(name,map);
	}

	public Map getFirstRech(Map map)throws Exception {
		//查询充值用户
		List<BirthdayModelDto> rechUsers = this.birthdayDao.getValueBySqlName1("rechUser",map);
		List<BirthdayModelDto> results = new ArrayList<BirthdayModelDto>();
		Double count = 0d,price=0d;
		for (BirthdayModelDto rechUser : rechUsers) {
			if(this.birthdayDao.getValueBySqlName1("rechUserHistory",FrameUtil.newHashMap("start",rechUser.getAddtime(),"user_id",rechUser.getUser_id())).size()==0){
				logger.info("生日礼包首充用户："+rechUser.getUser_id()+"："+rechUser.getPay_money());
				count++;
				price += rechUser.getPay_money();
			}
		}

		return FrameUtil.newHashMap("count",count,"price",price);
	}

}
